<template>
  <div class="code-system">
    <div class="headerMenu">
      <div class="input-box"><Input v-model="id" style="width: 200px">
        <span slot="prepend">ID</span>
      </Input></div>
      <Button :loading="loading" @click="handleSearch2" icon="ios-search" type="primary">
        查询术语
      </Button>
      <Button :loading="loading" @click="handleSearch3" icon="ios-search" type="warning">
        查询值集
      </Button>
      婚姻术语：id123654 已婚值集：id321654  婚姻值集：id321655
    </div>
    <Table :data="codeSystem" :columns="column" :loading="loading"></Table>
  </div>
</template>

<script>
  export default {
    name: 'CodeSystem',
    data() {
      return {
        resource: 'codeSystem',
        type: 'CodeSystem',
        searchForm: {},
        loading: false,
        id: 'id123654',
        column: [
          {
            label: '#',
            width: 50,
            type: 'index'
          },
          {
            title: '字典',
            key: 'codeSystem'
          },
          {
            title: '编码',
            key: 'code'
          },
          {
            title: '名称',
            key: 'name'
          },
        ]
      }
    },
    computed: {
      codeSystem() {
        let jData = []
        this.$store.state.medication.medications.forEach(entry => {
          console.log(entry)
          let re = entry.resource
          if (this.type == 'CodeSystem') {
            let concept = []
            if (re.total && re.total.myStringValue == '0') return
            concept = re.concept && re.concept.length ? re.concept : []
            jData = jData.concat(concept.map(item => {
              return {
                codeSystem: re.name ? re.name.myStringValue : '',
                name: item.display ? item.display.myStringValue : '',
                code: item.code ? item.code.myStringValue : ''
              }
            }))
          }
          if (this.type == 'ValueSet') {
            if (re.compose && re.compose.include && re.compose.include[0] && re.compose.include[0].concept) {
              re.compose.include[0].concept.map(item => {
                jData.push({
                  codeSystem: re.name ? re.name.myStringValue : '',
                  name: item.display ? item.display.myStringValue : '',
                  code: item.code ? item.code.myStringValue : ''
                })
              })
            }
          }
        })
        return jData
      }
    },
    created() {
      this.doSearch()
    },
    methods: {
      doSearch() {
        this.loading = true
        this.$store
          .dispatch('getMedicationList', {
            id: this.id,
            type: this.type
          })
          .then(() => {
            console.log('res')
            this.loading = false
          })
      },
      handleSearch2() {
        this.type = 'CodeSystem'
        this.doSearch()
      },
      handleSearch3() {
        this.type = 'ValueSet'
        this.doSearch()
      }
    },
  }
</script>

<style lang="less" scoped>
  .input-box {
    display: inline-block;
    vertical-align: middle;
  }
  .headerMenu{
    margin-bottom: 10px;
  }
</style>
